// stores/counter.js
import {
  defineStore
} from 'pinia'
import {
  ref,
  computed
} from 'vue'
import {
  serverpath
} from '@/utils/const.js';
// 1. 选项式 Store

// 这里省略上一小节代码

// 2. 组合式 Store
export const useUsercodeStore = defineStore('usercode', () => {
  // 定义 state
  const usercode = ref({
    token: '',
    userId: '',
    isAddress: 0,
    user: '',
    nickname: '',
    himg: '',
    animation: '',
    firstTX: '',
    firstWX: '',
    prompt: 0,
    mobile: '',
    isTrans: '',
    is_effect: '0'
  })

  // 定义 getters
  // const setAddress = computed((add) => usercode.value.isAddress = add)

  function setAddress(add) {
    usercode.value.isAddress = add
  }

  function setAnimation(add) {
    usercode.value.animation = add
  }

  function setHimg(add) {
    usercode.value.himg = add
  }

  function setNickname(add) {
    usercode.value.nickname = add
  }

  function setFirstTX(add) {
    usercode.value.firstTX = add
  }

  function setFirstWX(add) {
    usercode.value.firstWX = add
  }

  function resetToken() {
    usercode.value.token = ''
    usercode.value.userId = ''
    usercode.value.user = ''
  }

  function setPrompt(add) {
    usercode.value.prompt = add
  }

  function setIsTrans(add) {
    usercode.value.isTrans = add
  }

  // 定义 actions
  function setUsercode(res) {
    const pid = uni.getStorageSync('pid') || ''
    console.log(pid);
    const accountInfo = uni.getAccountInfoSync();
    const appid = accountInfo.miniProgram.appId;
    console.log(appid)
    // "appid": "wx76247eb634bbeab7", // 欧魂
    // "appid": "wx4761bcb230102f0a", // 欧到底
    // "appid": "wx1b72263f91db6ecd", //欧魂0000
    let urlString = ''
    if (appid == 'wx76247eb634bbeab7') {
      urlString = 'empowernew'
    }
    if (appid == 'wx4761bcb230102f0a') {
      urlString = 'empowernew10_18'
    }
    if (appid == 'wx1b72263f91db6ecd') {
      urlString = 'empowermra'
    }
    uni.request({
      // url: `${serverpath}api/empowernew/empower`, // 欧魂小程序登录后端接口URL地址
      url: `${serverpath}api/${urlString}/empower`,
      method: 'POST', // 请求方法，根据后端API可以选择'GET'、'POST'等
      data: {
        code: res.code,
        mobile_code: res.mobile,
        pid: pid
      },
      success: (res) => {
        // 请求成功的回调函数
        if (res.data.st == 1) {
          usercode.value.isAddress = res.data.data.is_address
          usercode.value.token = res.data.data.token
          usercode.value.userId = res.data.data.id
          usercode.value.user = res.data.data.user_id
          usercode.value.himg = res.data.data.himg
          usercode.value.animation = res.data.data.is_animation
          usercode.value.nickname = res.data.data.nickname
          usercode.value.firstTX = res.data.data.first_tx
          usercode.value.firstWX = res.data.data.first_wx
          usercode.value.mobile = res.data.data.mobile
          usercode.value.isTrans = res.data.data.is_trans
          usercode.value.is_effect = +res.data.data.is_effect
          uni.reLaunch({
            url: '/pages/home/index',
          })
        } else {
          uni.utils.toast(res.data.msg)
        }
      },
      fail: (err) => {
        // 请求失败的回调函数
        uni.utils.toast(err.errMsg)
        console.error(err)
      },
    })
  }

  function appSetUsercode(res) {
    uni.request({
      // url: 'https://https://ouhun.wm2177.com/api/empower/loginPost', // 后端接口URL地址
      url: `${serverpath}api/empower/loginPost`, // 测试后端接口URL地址
      // url: 'https://app.oujiang.fun/api/empower/loginPost', // 后端接口URL地址
      method: 'POST', // 请求方法，根据后端API可以选择'GET'、'POST'等
      data: {
        code: res.code,
        mobile: res.mobile,
      },
      success: (res) => {
        // 请求成功的回调函数
        console.log(res.data)
        if (res.data.st == 1) {
          usercode.value.isAddress = res.data.data.is_address
          usercode.value.token = res.data.data.token
          usercode.value.userId = res.data.data.id
          usercode.value.user = res.data.data.user_id
          usercode.value.himg = res.data.data.himg
          usercode.value.animation = res.data.data.is_animation
          usercode.value.nickname = res.data.data.nickname
          usercode.value.firstTX = res.data.data.first_tx
          usercode.value.firstWX = res.data.data.first_wx
          usercode.value.mobile = res.data.data.mobile
          usercode.value.isTrans = res.data.data.is_trans
          usercode.value.is_effect = res.data.data.is_effect
          uni.reLaunch({
            url: '/pages/home/index',
          })
        } else {
          uni.utils.toast(res.data.msg)
        }
      },
      fail: (err) => {
        // 请求失败的回调函数
        uni.utils.toast(err.errMsg)
        console.error(err)
      },
    })
  }


  function appPassword(res) {
    uni.request({
      url: `${serverpath}api/empower/passwordLogin`, // 后端接口URL地址
      method: 'POST', // 请求方法，根据后端API可以选择'GET'、'POST'等
      data: {
        mobile: res.code,
        password: res.password,
      },
      success: (res) => {
        // 请求成功的回调函数
        console.log(res.data.data.is_effect,
          'res.datares.datares.datares.datares.datares.datares.datares.datares.datares.data')
        if (res.data.st == 1) {
          usercode.value.isAddress = res.data.data.is_address
          usercode.value.token = res.data.data.token
          usercode.value.userId = res.data.data.id
          usercode.value.user = res.data.data.user_id
          usercode.value.himg = res.data.data.himg
          usercode.value.animation = res.data.data.is_animation
          usercode.value.nickname = res.data.data.nickname
          usercode.value.firstTX = res.data.data.first_tx
          usercode.value.firstWX = res.data.data.first_wx
          usercode.value.mobile = res.data.data.mobile
          usercode.value.isTrans = res.data.data.is_trans
          usercode.value.is_effect = res.data.data.is_effect
          uni.reLaunch({
            url: '/pages/home/index',
          })
        } else {
          uni.utils.toast(res.data.msg)
        }
      },
      fail: (err) => {
        // 请求失败的回调函数
        uni.utils.toast(err.errMsg)
        console.error(err)
      },
    })
  }

  function delUsercode() {
    usercode.value.token = ''
    usercode.value.userId = ''
  }

  // 千万不要忘记这里要 return
  return {
    useUsercodeStore,
    appSetUsercode,
    appPassword,
    usercode,
    setUsercode,
    delUsercode,
    setAddress,
    setAnimation,
    resetToken,
    setHimg,
    setNickname,
    setFirstTX,
    setFirstWX,
    setPrompt,
    setIsTrans
  }
}, {
  persist: {
    path: ['usercode']
  },
})